// The root URL for the RESTful services
var rootURL = "http://127.0.0.1/cancha/api/index.php/canchas";

// Retrieve wine list when application starts 
findAll();


function findAll() {
	console.log('findAll');
	$.ajax({
		type: 'GET',
		url: rootURL,
		dataType: "json", // data type of response
		success: renderList
	});
}

function GetById(id) {
    console.log('GetById: ' + id);
    $.ajax({
            type: 'GET',
            url: "http://127.0.0.1/cancha/api/index.php/canchas/1",
            dataType: "json",
            success: function(data){
                    $('#btnDelete').show();
                    console.log('findById success: ' + data.name);
                    data = 1;
                    renderDetails;
            }
    });
}

function renderDetails(data) {
    $('#cuenta_id').append(data.id_usuario);
    $('#cuenta_saldo').append(data.cuenta);
}


function renderList(data) {
	// JAX-RS serializes an empty list as null, and a 'collection of one' as an object (not an 'array of one')
	var list = data == null ? [] : (data.canchas instanceof Array ? data.canchas : [data.canchas]);

	$('#canchaList li').remove();
	$.each(list, function(index, canchas) {
		$('#canchaList').append('<li><a href="#" data-identity="' + canchas.id + '">'+canchas.HORA+'</a></li>');
		$('#canchaListL').append('<li><a href="#" data-identity="' + canchas.id + '">'+canchas.LUNES+'</a></li>');
		$('#canchaListM').append('<li><a href="#" data-identity="' + canchas.id + '">'+canchas.MARTES+'</a></li>');
		$('#canchaListX').append('<li><a href="#" data-identity="' + canchas.id + '">'+canchas.MIERCOLES+'</a></li>');
		$('#canchaListJ').append('<li><a href="#" data-identity="' + canchas.id + '">'+canchas.JUEVES+'</a></li>');
		$('#canchaListV').append('<li><a href="#" data-identity="' + canchas.id + '">'+canchas.VIERNES+'</a></li>');
		$('#canchaListS').append('<li><a href="#" data-identity="' + canchas.id + '">'+canchas.SABADO+'</a></li>');
		$('#canchaListD').append('<li><a href="#" data-identity="' + canchas.id + '">'+canchas.DOMINGO+'</a></li>');
	});
}



